var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));
var Tx = require('ethereumjs-tx').Transaction;

//黑窗口第一个地址
var fromAddr = "0xF56490718203bd971f2EaB5FAc3f2ec95f47a828";
//第一个地址的私钥
var privateKey = Buffer.from('f410b05139f6a1ffa411ff31a46e231ca43194288e0b3ffe23d117aceb9c007f', 'hex')
//合约地址
var contractAddr = '0x09fB81C35ba0C933B9b310fDaEB0dCEDDcd5A242';
//wzabi文件
var abi = [{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_receivers","type":"address[]"},{"name":"_value","type":"uint256"}],"name":"batchTransfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"frozenAccount","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"freeze","type":"bool"}],"name":"freezeAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"target","type":"address"},{"indexed":false,"name":"frozen","type":"bool"}],"name":"FrozenFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];

//构造函数:
var wzContract = new web3.eth.Contract(abi,contractAddr);

//getALLPastEvents();

function getALLPastEvents(){
	wzContract.getPastEvents('allEvents', {
	    fromBlock: 0,
	    toBlock: 'latest'
	}, function(error, events){ 
		if(error){
			console.log(error);
		}
		})
	.then(function(events){
		for(var event of events){
		    console.log("transactionHash"+":"+event.transactionHash)
			console.log("blockHash"+":"+event.blockHash)
			console.log("blockNumber"+":"+event.blockNumber)
			console.log("address"+":"+event.address)
			console.log("from"+":"+event.returnValues.from)
			console.log("to"+":"+event.returnValues.to)
			console.log("value"+":"+event.returnValues.value)
		}
	    
	});
}

getTransaction("0x494f0d01275a585b93f84db74a48a847b82e7574d3679318c6f9ec37fdb983ab");

function getTransaction(Transaction){
	web3.eth.getTransaction(Transaction,function(err,events){
		if(err){
			console.log(err);
		}else{
			console.log("hash"+":"+events.hash)
			console.log("to"+":"+events.to)
			console.log("from"+":"+events.from)
			console.log("blockNumber"+":"+events.blockNumber)
			console.log("value"+":"+events.value)
			console.log("gasprice"+":"+events.gasPrice)
			console.log("gas"+":"+events.gas)
		}
	})
}

